package cn.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;


/**
 * 
 * @author 杏
 *
 */
public class DateUtil {
	public static final String yyMMdd = "yy-MM-dd";
	public static final String yyyyMMdd = "yyyy-MM-dd";
	public static final String HHmmss = "HH:mm:ss";
	public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
	public static final String yyMMddHHmmss = "yy-MM-dd HH:mm:ss";
	public static String parseToString(Date date, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		String str = null;
		if (date == null)
			return null;
		str = simpleDateFormat.format(date);
		return str;
	}
	public static Date parseToDate(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		Date date = null;
		if (s == null || s.length() < 8)
			return null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 获取当前日期
	 * @return
	 */
	public static String getcurrentime() {
		/*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime_1 = new Date();
		String dateString = formatter.format(currentTime_1);*/
         //上面的方法返回的是2008-06-06这个格式,下面的返回2008-6-6,后者在sql server的"<"或">"使用,前者不能进行这种比较
		GregorianCalendar currentDay = new GregorianCalendar(); 
		int today=currentDay.get(Calendar.DAY_OF_MONTH);
		int month=currentDay.get(Calendar.MONTH)+1;
		int year= currentDay.get(Calendar.YEAR); 
		String dateString=""+year+"-"+month+"-"+today+"";
		return dateString;
	}
	
	public static String getcurrentimeTwo() {
		/*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime_1 = new Date();
		String dateString = formatter.format(currentTime_1);*/
         //上面的方法返回的是2008-06-06这个格式,下面的返回2008-6-6,后者在sql server的"<"或">"使用,前者不能进行这种比较
		GregorianCalendar currentDay = new GregorianCalendar(); 
		int today=currentDay.get(Calendar.DAY_OF_MONTH);
		int month=currentDay.get(Calendar.MONTH)+1;
		int year= currentDay.get(Calendar.YEAR); 
		String dateString=""+year+""+month+""+today+"";
		return dateString;
	}
	
	/**
	 * 获取当前年
	 * @return
	 */
	public static String getCurrentYear(){
		GregorianCalendar currentDay = new GregorianCalendar(); 
		return String.valueOf(currentDay.get(Calendar.YEAR));
	}
	
	public static String getcurrenttime_total(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentTime_1 = new Date();
		return formatter.format(currentTime_1);
		//Date date=currentDay.get(Calendar.DATE);
	}
	
	public static Date getcurrenttime(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentTime_1 = new Date();
		return currentTime_1;
		//Date date=currentDay.get(Calendar.DATE);
	}
	public static String getwaitThreetime() {
		/*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime_1 = new Date();
		String dateString = formatter.format(currentTime_1);*/
         //上面的方法返回的是2008-06-06这个格式,下面的返回2008-6-6,后者在sql server的"<"或">"使用,前者不能进行这种比较
		GregorianCalendar currentDay = new GregorianCalendar(); 
		int today=currentDay.get(Calendar.DAY_OF_MONTH);
		int month=currentDay.get(Calendar.MONTH)+1;
		int year= currentDay.get(Calendar.YEAR); 
		String dateString=""+year+"-"+month+"-"+today+"";
		return dateString;
	}
	
	//获取一个分别封装当前时间的年/月/日的MAP
	public static Map<String,String> getCurrentMap(){
		GregorianCalendar currentDay = new GregorianCalendar(); 
		Integer today=currentDay.get(Calendar.DAY_OF_MONTH);
		String  s_today =String.valueOf(today);
		if(s_today.length()==1){
			s_today="0"+s_today;
		}
		Integer month=currentDay.get(Calendar.MONTH)+1;
		String s_month = String.valueOf(month);
		if(s_month.length()==1){
			s_month="0"+s_month;
		}
		Integer year= currentDay.get(Calendar.YEAR); 
		String s_year = String .valueOf(year);
		Map<String,String>  m =  new HashMap<String,String> ();
		m.put("year", s_year);
		m.put("month", s_month);
		m.put("day", s_today);		
		return m;
	}
	//格式化一个特定时间，封装为一个Map
	public static Map<String,String>getCurrentMap(Date objDate){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(objDate);
		Integer day=cal.get(Calendar.DAY_OF_MONTH);
		String  s_today =String.valueOf(day);
		if(s_today.length()==1){
			s_today="0"+s_today;
		}
		Integer month=cal.get(Calendar.MONTH)+1;
		String s_month = String.valueOf(month);
		if(s_month.length()==1){
			s_month="0"+s_month;
		}
		Integer year= cal.get(Calendar.YEAR);
		String s_year = String .valueOf(year);
		Map<String,String>  m =  new HashMap<String,String> ();
		m.put("year", s_year);
		m.put("month", s_month);
		m.put("day", s_today);		
		return m;
	}
	
	/**
	 * 计算两个时间差
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public static long subtractDate(Date dateStart,Date dateEnd){
		long obj = dateEnd.getTime() - dateStart.getTime();		
		return obj;
	}
	
	/**
	 * 时间增加
	 * @param date
	 * @param time
	 * @return
	 */
	public static Date addDayTime(Date date, Long time){
		return new Date(date.getTime()+1000*60*60*24);
	}
	
	public static void main(String[] args) {
		DateUtil du=new DateUtil();
		System.out.println(DateUtil.getcurrentimeTwo());
		System.out.println(du.getcurrenttime_total());
		System.out.println(du.getcurrenttime_total().substring(11));
		Date date = new Date(du.getcurrenttime().getTime()+1000*60*60*24);
		System.out.println(du.parseToString(date, du.yyyyMMddHHmmss));
		
		//Map<String,String> m=du.getCurrentMap();
	//	System.out.println(m.get("year"));
		//System.out.println(m.get("month"));
		//System.out.println(m.get("day"));
	}	
}
